import { ref } from 'vue';
//引入axios
import axios from 'axios';
//发送ajax的请求
export default function <T>(url: string) {
  const loading = ref(true);//加载的状态
  const data = ref<T | null>(null);//请求成功的数据
  const errorMsg = ref('');//错误信息

  //发送请求
  axios.get(url).then(res => {
    //改变加载状态
    loading.value = false;
    data.value = res.data;
  }).catch(error => {
    loading.value = false;
    errorMsg.value = error.messsage || '未知错误';
  })
  return {
    loading,
    data,
    errorMsg
  }
}